package com.tencent.wxcloudrun.config;

import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Data
public final class SuccessResult {
    private boolean success;
    private String message;
    private Object data;
    private Object root;
    private Long total;

    private SuccessResult(boolean success, String message, Object data, Object root, Long total) {
        this.success = success;
        this.message = message;
        this.data = data;
        this.root = root;
        this.total = total;
    }

    /**
     * @apiNote 搜索成功
     * @param result 搜索内容
     * @return 通用返回类
     */
    public static SuccessResult SelectSuccess (Map<String, Object> result) {
        return new SuccessResult(
                ResultEnum.SEARCH_SUCCESS.getSuccess(),
                ResultEnum.SEARCH_SUCCESS.getMessage(),
                result.get("data"),
                new ArrayList<>(),
                Long.parseLong(result.get("total").toString())
        );
    }

    /**
     * @apiNote 搜索失败
     * @return 通用返回类
     */
    public static SuccessResult SelectFailed () {
        return new SuccessResult(
                ResultEnum.SEARCH_FAILED.getSuccess(),
                ResultEnum.SEARCH_FAILED.getMessage(),
                new HashMap<>(),
                new ArrayList<>(),
                0L
        );
    }

    /**
     * @apiNote 更新成功
     * @return 通用返回类
     */
    public static SuccessResult UpdateSuccess () {
        return new SuccessResult(
                ResultEnum.UPDATE_SUCCESS.getSuccess(),
                ResultEnum.UPDATE_SUCCESS.getMessage(),
                new HashMap<>(),
                new ArrayList<>(),
                0L
        );
    }

    /**
     * @apiNote 更新失败
     * @return 通用返回类
     */
    public static SuccessResult UpdateFailed () {
        return new SuccessResult(
                ResultEnum.UPDATE_FAILED.getSuccess(),
                ResultEnum.UPDATE_FAILED.getMessage(),
                new HashMap<>(),
                new ArrayList<>(),
                0L
        );
    }

    /**
     * @apiNote 删除成功
     * @return 通用返回类
     */
    public static SuccessResult DeleteSuccess () {
        return new SuccessResult(
                ResultEnum.DELETE_SUCCESS.getSuccess(),
                ResultEnum.DELETE_SUCCESS.getMessage(),
                new HashMap<>(),
                new ArrayList<>(),
                0L
        );
    }

    /**
     * @apiNote 更新失败
     * @return 通用返回类
     */
    public static SuccessResult DeleteFailed () {
        return new SuccessResult(
                ResultEnum.DELETE_FAILED.getSuccess(),
                ResultEnum.DELETE_FAILED.getMessage(),
                new HashMap<>(),
                new ArrayList<>(),
                0L
        );
    }

    /**
     * @apiNote 登陆成功
     * @param result 返回参数
     * @return 通用返回类
     */
    public static SuccessResult LoginSuccess (Object result) {
        return new SuccessResult(
                ResultEnum.LOGIN_SUCCESS.getSuccess(),
                ResultEnum.LOGIN_SUCCESS.getMessage(),
                result,
                new ArrayList<>(),
                0L
        );
    }

    /**
     * @apiNote 用户名不存在
     * @return 通用返回类
     */
    public static SuccessResult UserNameUndefined () {
        return new SuccessResult(
                ResultEnum.USERNAME_UNDEFINED.getSuccess(),
                ResultEnum.USERNAME_UNDEFINED.getMessage(),
                new HashMap<>(),
                new ArrayList<>(),
                0L
        );
    }

    /**
     * @apiNote 密码错误
     * @return 通用返回类
     */
    public static SuccessResult PasswordFailed () {
        return new SuccessResult(
                ResultEnum.PASSWORD_FAILED.getSuccess(),
                ResultEnum.PASSWORD_FAILED.getMessage(),
                new HashMap<>(),
                new ArrayList<>(),
                0L
        );
    }

    /**
     * @apiNote 注册成功
     * @return 通用返回类
     */
    public static SuccessResult SignInSuccess () {
        return new SuccessResult(
                ResultEnum.SIGN_SUCCESS.getSuccess(),
                ResultEnum.SIGN_SUCCESS.getMessage(),
                new HashMap<>(),
                new ArrayList<>(),
                0L
        );
    }

    /**
     * @apiNote 注册失败
     * @return 通用返回类
     */
    public static SuccessResult SignInFailed () {
        return new SuccessResult(
                ResultEnum.SIGN_FAILED.getSuccess(),
                ResultEnum.SIGN_FAILED.getMessage(),
                new HashMap<>(),
                new ArrayList<>(),
                0L
        );
    }

    /**
     * @apiNote 系统错误
     * @param e 错误信息
     * @return 通用返回类
     */
    public static SuccessResult SystemError (Object e) {
        return new SuccessResult(
                ResultEnum.SYSTEM_ERROR.getSuccess(),
                ResultEnum.SYSTEM_ERROR.getMessage(),
                e,
                new ArrayList<>(),
                0L
        );
    }
}
